package com.lzy.imagepickerdemo.zn;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.lzy.imagepicker.ImageDataSource;
import com.lzy.imagepicker.bean.ImageFolder;
import com.lzy.imagepicker.ui.ImageBaseActivity;
import com.lzy.imagepickerdemo.GApp;
import com.lzy.imagepickerdemo.R;
import com.lzy.imagepickerdemo.zn.adapter.ZnAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhotoListActivity extends ImageBaseActivity implements ImageDataSource.OnImagesLoadedListener {

    ZnAdapter mZnAdapter;
    @Bind(R.id.rev_photo_list)
    RecyclerView revPhotoList;
    int postion;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_del)
    TextView tvDel;

    public static void start(Context context, int postion,String name) {
        context.startActivity(new Intent(context, PhotoListActivity.class).putExtra("postion", postion).putExtra("name",name));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_list);
        ButterKnife.bind(this);
        postion = getIntent().getIntExtra("postion", 0);
        tvName.setText(getIntent().getStringExtra("name"));
        mZnAdapter = new ZnAdapter();
        revPhotoList.setLayoutManager(new LinearLayoutManager(this));
        revPhotoList.setAdapter(mZnAdapter);
        try {
            mZnAdapter.setNewData(GApp.getInstance().mDBManager.getPhotolist(postion));
        } catch (Exception e) {
            e.printStackTrace();
        }
       // ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1001) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                new ImageDataSource(PhotoListActivity.this, null, this);
            } else {
                showToast("权限被禁止，无法选择本地图片");
            }
        }
    }

    @Override
    public void onImagesLoaded(List<ImageFolder> mimageFolders) {
        /*if (postion==0){
            mZnAdapter.setNewData(mimageFolders.get(1).images);
        }else{
            mZnAdapter.setNewData(mimageFolders.get(2).images);
        }*/

    }

    @OnClick(R.id.tv_del)
    public void onViewClicked() {
        GApp.getInstance().mDBManager.delPhotoType(postion);
        finish();
    }
}
